package com.yoda.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yoda.constant.MessageConstant;
import com.yoda.dto.MenuDto;
import com.yoda.entity.SysMenu;
import com.yoda.mapper.SysMenuMapper;
import com.yoda.result.Result;
import com.yoda.service.ISysMenuService;
import com.yoda.vo.MenuList;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-05-15
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;


    /**
     * 菜单修改
     * @param dto
     * @return
     */
    @Override
    public Result updateMenu(MenuDto dto) {
        //非空判断
        if (dto.getId() == null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //根据id获取到原来的所有数据再复制过去
        SysMenu menu = this.getById(dto.getId());
        BeanUtils.copyProperties(dto,menu);
        //添加条件
        LambdaQueryWrapper<SysMenu> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysMenu::getId , dto.getId());
        //调用方法修改
        this.saveOrUpdate(menu,wrapper);
        return Result.success();
    }

    /**
     * 根据id删除菜单
     * @param id
     * @return
     */
    @Override
    public Result deleteById(String id) {
        //调用方法直接删除
        boolean remove = this.removeById(id);
        return Result.success(remove);
    }

    /**
     * 查询所有菜单
     * @return
     */
    @Override
    public Result getMenu() {
        //直接查询所有然后返回
        List<MenuList> menu = sysMenuMapper.getMenu(0);
        for (MenuList menuList : menu) {
            List<MenuList> lists = sysMenuMapper.getMenu(menuList.getId());
            menuList.setChildren(lists);
            for (MenuList list : lists) {
                List<MenuList> menuLists = sysMenuMapper.getMenu(list.getId());
                list.setChildren(menuLists);
            }
        }
        return Result.success(menu);
    }

    /**
     * 菜单添加
     * @param dto
     * @return
     */
    @Override
    public Result addMenu(MenuDto dto) {

        //新建一个SysMenu对象将收到的内容复制到对象中
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(dto,sysMenu);
        sysMenu.setCreateDate(LocalDateTime.now());
        sysMenu.setSort(BigDecimal.valueOf(dto.getSort()));
        sysMenuMapper.insert(sysMenu);
        return Result.success();
    }

    /**
     * 菜单详情
     * @param id
     * @return
     */
    @Override
    public Result selectById(String id) {
        //非空判断
        if (id == null){
            return Result.error(MessageConstant.ID_NOT_FOUND);
        }
        //直接根据id单一查询，然后返回
        SysMenu menu = this.getById(id);
        return Result.success(menu);
    }



}
